package com.ruoyi.web.controller.video;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.video.VsLangTranslate;
import com.ruoyi.system.service.video.IVsLangTranslateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
/**
 * This file is part of 南阳迈特网络科技有限公司.
 * @company  南阳迈特网络科技有限公司
 * @link     https://www.nymaite.com
 * @document https://product.nymaite.cn
 * @contact  service@nymaite.com
 * @author nymaite
 */
/**
 * 语言对照Controller
 * 
 * @author zzx
 * @date 2024-04-17
 */
@Api(tags = "语言对照")
@RestController
@RequestMapping("/video/translate")
public class VsLangTranslateController extends BaseController
{
    @Autowired
    private IVsLangTranslateService vsLangTranslateService;

    /**
     * 查询语言对照列表
     */
//    @ApiOperation("查询语言对照列表")
    @GetMapping("/list")
    public TableDataInfo list(VsLangTranslate vsLangTranslate)
    {
        List<VsLangTranslate> list = vsLangTranslateService.selectVsLangTranslateList(vsLangTranslate);
        return getDataTable(list);
    }

    /**
     * 导出语言对照列表
     */
//    @ApiOperation("导出语言对照列表")
    @Log(title = "语言对照", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VsLangTranslate vsLangTranslate)
    {
        List<VsLangTranslate> list = vsLangTranslateService.selectVsLangTranslateList(vsLangTranslate);
        ExcelUtil<VsLangTranslate> util = new ExcelUtil<VsLangTranslate>(VsLangTranslate.class);
        util.exportExcel(response, list, "语言对照数据");
    }

    /**
     * 获取语言对照详细信息
     */
//    @ApiOperation("获取语言对照详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(vsLangTranslateService.selectVsLangTranslateById(id));
    }

    /**
     * 新增语言对照
     */
//    @ApiOperation("新增语言对照")
    @Log(title = "语言对照", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VsLangTranslate vsLangTranslate)
    {
        return toAjax(vsLangTranslateService.insertVsLangTranslate(vsLangTranslate));
    }

    /**
     * 修改语言对照
     */
//    @ApiOperation("修改语言对照")
    @Log(title = "语言对照", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VsLangTranslate vsLangTranslate)
    {
        return toAjax(vsLangTranslateService.updateVsLangTranslate(vsLangTranslate));
    }

    /**
     * 删除语言对照
     */
//    @ApiOperation("删除语言对照")
    @Log(title = "语言对照", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(vsLangTranslateService.deleteVsLangTranslateByIds(ids));
    }

    /**
     * 批量插入语言对照
     */
//    @ApiOperation("批量修改语言对照")
    @Log(title = "语言对照", businessType = BusinessType.UPDATE)
    @PostMapping("/batchInsert")
    public AjaxResult batchInsert(@RequestBody List<VsLangTranslate> list)
    {
        return toAjax(vsLangTranslateService.batchInsertVsLangTranslate(list));
    }

    /**
     * 查询语言对照列表
     */
    @ApiOperation("查询语言对照列表")
    @GetMapping("/langList")
    public AjaxResult languageList(VsLangTranslate vsLangTranslate, HttpServletRequest request)
    {
        if (!StringUtils.isEmpty(request.getHeader("Lang-Id"))) {
            Integer integer = Integer.valueOf(request.getHeader("Lang-Id"));
            vsLangTranslate.setLangId(integer);
        }
        List<VsLangTranslate> list = vsLangTranslateService.selectVsLangTranslateList(vsLangTranslate);
        Map<String, String> map = list.stream()
                .filter(entity -> entity.getKeyCode() != null)
                .collect(Collectors.toMap(
                        VsLangTranslate::getKeyCode,
                        VsLangTranslate::getLanguage,
                        (oldValue, newValue) -> newValue // 使用新值覆盖旧值
                ));
        return success(map);
    }

    @PostMapping("/batchTranslate")
    public AjaxResult batchTranslate(@RequestBody List<VsLangTranslate> list) {
        List<VsLangTranslate> translateList = vsLangTranslateService.batchTranslate(list);
        return success(translateList);
    }

}
